package org.ccunix.eshop.controller.api;

import com.alibaba.fastjson.JSON;
import org.ccunix.eshop.domain.dto.CartSelectedMerVO;
import org.ccunix.eshop.domain.dto.CartVO;
import org.ccunix.eshop.domain.dto.MemberVO;
import org.ccunix.eshop.domain.dto.OrdersVO;
import org.ccunix.eshop.service.OrdersServiceIface;
import org.ccunix.eshop.service.ShopCarServiceIface;
import org.ccunix.eshop.service.impl.OrdersServiceImpl;
import org.ccunix.eshop.service.impl.ShopCarServiceImpl;
import org.ccunix.eshop.utils.AjaxResult;
import org.ccunix.eshop.utils.SessionUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/auth/orders")
public class OrdersController extends HttpServlet {
    OrdersServiceIface ordersService = new OrdersServiceImpl();
    ShopCarServiceIface shopCarService = new ShopCarServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("m");
        if ("list".equals(method)){
            list(request,response);
        }else if ("delete".equals(method)){
            deleteOrdersById(request,response);
        }else if ("detail".equals(method)){
            orderDetail(request,response);
        }else if ("merchantList".equals(method)){
            merchantList(request, response);
        }
    }

    //订单详情页面
    private void orderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        AjaxResult result = AjaxResult.success("查询单个订单成功");
        OrdersVO ordersVO = ordersService.getOrderById(id);
        result.put("data",ordersVO);
        result.put("data1",ordersVO);
        response.getWriter().write(JSON.toJSONString(result));
    }

    //订单中的商品详情
    private void merchantList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        //获得商品信息
        OrdersVO ordersVO = ordersService.getOrderById(id);
        AjaxResult result = AjaxResult.success("订单中商品详情列表列表获取成功");
        List<CartSelectedMerVO> cartSelectedMerList = shopCarService.getCartSelectedMerListByCar(ordersVO.getCart());//根据购物车去查询购物详情
        result.put("data",cartSelectedMerList);
        response.getWriter().write(JSON.toJSONString(result));
    }
    //删除订单信息
    private void deleteOrdersById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        AjaxResult result = AjaxResult.success("订单删除成功");
        boolean b = ordersService.deleteOrdersById(id);
        result.put("data",b);
        response.getWriter().write(JSON.toJSONString(result));
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //订单管理列表
            AjaxResult result = AjaxResult.success("订单列表获取成功");
            //获得登录信息
            MemberVO memberVO = SessionUtil.getLoginMemberInfo(request);
            List<OrdersVO> ordersList = ordersService.getOrdersListByMid(memberVO.getId());//根据会员id
            //获得购物车信息
            result.put("data",ordersList);
            response.getWriter().write(JSON.toJSONString(result));
        }
}
